Skip to content

feat: add enforceUniqueInstanceID option when scheduling workflows - #116

Open
javier-aliaga wants to merge 1 commit into
dapr:mainfrom
javier-aliaga:feat/schedule-if-not-exists
Open

feat: add enforceUniqueInstanceID option when scheduling workflows#116
javier-aliaga wants to merge 1 commit into
dapr:mainfrom
javier-aliaga:feat/schedule-if-not-exists

Conversation

@javier-aliaga

@javier-aliaga javier-aliaga commented Jul 14, 2026

Copy link
Copy Markdown

Adds an enforceUniqueInstanceID option when scheduling a workflow. When set, scheduling fails with a gRPC ALREADY_EXISTS error if an instance with the same ID already exists — whether active or completed — so the caller decides how to handle it. Without it, behavior is unchanged (an active duplicate errors, a completed instance is restarted).

@javier-aliaga
javier-aliaga force-pushed the feat/schedule-if-not-exists branch from b0f6d81 to 4dcf91e Compare July 28, 2026 11:23
@javier-aliaga javier-aliaga changed the title feat: add scheduleIfNotExists option when scheduling workflows feat: add enforceUniqueInstanceID option when scheduling workflows Jul 28, 2026
@javier-aliaga
javier-aliaga force-pushed the feat/schedule-if-not-exists branch from 4dcf91e to 2629a47 Compare July 28, 2026 11:29
@javier-aliaga
javier-aliaga marked this pull request as ready for review July 28, 2026 11:32
@javier-aliaga
javier-aliaga requested a review from a team as a code owner July 28, 2026 11:32
@javier-aliaga
javier-aliaga force-pushed the feat/schedule-if-not-exists branch from 2629a47 to 452c493 Compare July 29, 2026 09:11
Signed-off-by: Javier Aliaga <javier@diagrid.io>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new scheduling option to enforce uniqueness of workflow instance IDs at schedule time, with corresponding gRPC error mapping and new tests to validate behavior.

Changes:

  • Add api.WithEnforceUniqueInstanceID() option to request ALREADY_EXISTS/duplicate-instance behavior on scheduling.
  • Thread enforceUniqueInstanceId through the Go client and gRPC executor into backend CreateWorkflowInstance.
  • Add/adjust tests (orchestration + gRPC) and update backend mocks/signatures to use a request wrapper.

Reviewed changes

Copilot reviewed 10 out of 11 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
api/orchestration.go Adds public scheduling option WithEnforceUniqueInstanceID() and docs.
backend/backend.go Updates backend interface to accept CreateWorkflowInstanceRequest.
backend/client.go Passes EnforceUniqueInstanceId into backend create call.
backend/executor.go Maps duplicate-instance errors to gRPC codes.AlreadyExists and threads flag through.
backend/sqlite/sqlite.go Updates backend create signature to accept request wrapper.
backend/postgres/postgres.go Updates backend create signature to accept request wrapper.
backend/client_router_test.go Updates fake backend signature for new request wrapper.
tests/backend_test.go Updates backend tests to pass request wrapper.
tests/mocks/Backend.go Updates generated mock to new request type.
tests/orchestrations_test.go Adds orchestration test for enforced-unique instance ID behavior.
tests/grpc/grpc_test.go Adds gRPC test asserting codes.AlreadyExists behavior.
Files not reviewed (1)
  • tests/mocks/Backend.go: Generated file
Suppressed comments (2)

backend/postgres/postgres.go:552

  • CreateWorkflowInstance now takes a CreateWorkflowInstanceRequest (which includes enforceUniqueInstanceId), but the current flow doesn’t consult that flag. Without checking it, api.WithEnforceUniqueInstanceID() can’t affect behavior. Consider using req.GetEnforceUniqueInstanceId() to decide whether to purge-and-retry when a completed instance already exists (and keep returning api.ErrDuplicateInstance when the existing instance is still running or uniqueness is enforced).
	e := req.GetStartEvent()

	tx, err := be.db.BeginTx(ctx, pgx.TxOptions{})
	if err != nil {
		return fmt.Errorf("failed to start transaction: %w", err)

backend/sqlite/sqlite.go:423

  • CreateWorkflowInstance now takes a CreateWorkflowInstanceRequest (which includes enforceUniqueInstanceId), but this implementation only uses StartEvent and ignores the uniqueness flag. That means api.WithEnforceUniqueInstanceID() can’t change behavior, and it also prevents implementing the documented default behavior (restart completed instances when uniqueness is not enforced). Consider using req.GetEnforceUniqueInstanceId() to decide whether to purge-and-retry on duplicates for completed instances.
	e := req.GetStartEvent()

	tx, err := be.db.BeginTx(ctx, nil)
	if err != nil {
		return fmt.Errorf("failed to start transaction: %w", err)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread backend/sqlite/sqlite.go
Comment on lines 418 to +420

e := req.GetStartEvent()

Comment thread api/orchestration.go
Comment on lines +105 to +108
// WithEnforceUniqueInstanceID configures scheduling to fail with an
// ALREADY_EXISTS error if an instance with the same ID already exists,
// whether active or completed. Without it, an existing completed instance
// is restarted.
Comment thread backend/backend.go
Comment on lines +65 to +67
// CreateWorkflowInstance creates a new workflow instance with a request that
// wraps a ExecutionStarted history event.
CreateWorkflowInstance(context.Context, *CreateWorkflowInstanceRequest) error
Comment on lines 547 to +549

e := req.GetStartEvent()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants